package site.wenneng.dev.controller;

import com.ruoyi.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.wenneng.dev.common.enumeration.DicEnum;
import site.wenneng.dev.objs.bo.StoryRemoteBO;
import site.wenneng.dev.objs.bo.TaskRemoteBO;
import site.wenneng.dev.objs.dto.StoryDTO;
import site.wenneng.dev.objs.dto.StoryTaskViewDTO;
import site.wenneng.dev.objs.dto.common.Result;
import site.wenneng.dev.objs.dto.progress.ProgressTrackingReqDTO;
import site.wenneng.dev.objs.po.DevUserPO;
import site.wenneng.dev.objs.po.WorkingHoursPO;
import site.wenneng.dev.service.*;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

@RestController
@RequestMapping("/progress/tracking")
@AllArgsConstructor
public class ProgressTrackingController {

    private TaskService taskService;
    private DevUserService userService;
    private WorkingHoursService workingHoursService;
    private HolidayService holidayService;
    private DictionaryService dictionaryService;
    private StoryService storyService;


    @GetMapping("/story")
    public Result<List<StoryTaskViewDTO>> queryUnfinishedStory(ProgressTrackingReqDTO inDTO) {
        // 查询小组成员
        List<DevUserPO> userList = inDTO.getAssigneeId().equals("-1") ? userService.findAll() :
                Collections.singletonList(new DevUserPO(inDTO.getAssigneeId()));
        // 需要排除的任务
        Set<String> dailyTaskSet = dictionaryService.listValueSet(DicEnum.DAILY_TASK);
        // 执行查询并按要求完成日期和预估工时排序
        List<TaskRemoteBO> taskList = taskService.queryUnfinishedTask(userList).stream()
                .filter(item -> !dailyTaskSet.contains(item.getTaskNumber()))
                .collect(Collectors.toList());

        List<String> storyNumbers = taskList.stream().map(TaskRemoteBO::getStoryNumbers)
                .filter(StringUtils::isNotBlank)
                .flatMap(item -> Arrays.stream(item.split(",")))
                .distinct().collect(Collectors.toList());
        List<StoryRemoteBO> storyList = storyService.queryStoryByNumbers(storyNumbers);

        return Result.ok(storyService.buildViewDTO(storyList, taskList));
    }

    @NotNull
    private List<StoryDTO> convertToDTO(List<StoryRemoteBO> storyRemoteList, Map<String, List<TaskRemoteBO>> taskMap) {
        List<StoryDTO> result = new ArrayList<>();
        Set<String> finishedTaskStatusSet = dictionaryService.listLabelSet(DicEnum.FINISHED_TASK_STATUS);
        for (StoryRemoteBO remoteBO : storyRemoteList) {
            StoryDTO storyDTO = new StoryDTO();
            storyDTO.setId(remoteBO.getId());
            storyDTO.setNumber(remoteBO.getNumber());
            storyDTO.setName(remoteBO.getName());
            storyDTO.setStatus(remoteBO.getStatus());
            storyDTO.setStoryLabels(remoteBO.getStoryLabels());
//            storyDTO.setSelfStoryHandlerList(toNameList(remoteBO.getStoryHandlerList()));
//            storyDTO.setSelfStoryDesignerNameList(toNameList(remoteBO.getStoryDesignerList()));
//            storyDTO.setSelfStoryReview(matchAny(remoteBO.getLabelList(), "需求评审-计划"));
//            storyDTO.setSelfTaskReview(matchAny(remoteBO.getLabelList(), "概要设计-计划"));
//            storyDTO.setSelfEstimateWorkload(matchWorkload(remoteBO.getRemark()));
            storyDTO.setSelfStatus(calcSelfStatus(taskMap.get(remoteBO.getNumber()), finishedTaskStatusSet));
//            storyDTO.setSelfActualDesignNameList(calcDesignNameList(taskMap.get(remoteBO.getNumber())));
//            storyDTO.setSelfActualDevelopNameList(calcDevelopNameList(taskMap.get(remoteBO.getNumber())));
//            storyDTO.setSelfPriority(calcPriority(remoteBO));
////            storyDTO.setSelfVersionFinish(calcVersionFinish(taskMap, finishedTaskStatusSet, remoteBO));
//            WorkingHoursPO workingHoursPO = workingHoursMap.get(remoteBO.getNumber());
//            if (workingHoursPO != null) {
//                storyDTO.setSelfUsedWorkload(workingHoursPO.getWorkingHours());
//            }
            result.add(storyDTO);
        }
        return result;
    }

    private String calcSelfStatus(List<TaskRemoteBO> taskRemoteList, Set<String> finishedTaskStatusSet) {
        if (CollectionUtils.isEmpty(taskRemoteList)) {
            return "待分解";
        }

        List<TaskRemoteBO> devTask = taskRemoteList.stream()
                .filter(item -> item.getTaskType() != 103)
                .collect(toList());
        List<TaskRemoteBO> unFinishedTask = taskRemoteList.stream()
                .filter(item -> !finishedTaskStatusSet.contains(item.getSubStatusName()))
                .collect(toList());
        Set<TaskRemoteBO> unFinishedDesignTask = unFinishedTask.stream()
                .filter(item -> item.getTaskType() == 103)
                .collect(toSet());
        if (unFinishedDesignTask.size() != 0 || devTask.size() == 0) {
            return "设计中";
        }
        if (unFinishedTask.size() != 0) {
            boolean isTesting = unFinishedTask.stream().allMatch(item -> "待审核".equals(item.getSubStatusName()));
            return isTesting ? "联测中" : "开发中";
        }
        return "已完成";
    }

}
